package com.ruoyi.quartz.task;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.V2.inter.mapper.*;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.model.domain.OutRec;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.domain.SelectPush;
import com.ruoyi.system.model.mapper.*;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.service.IOutApi;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class TimeUpTask {

    @Resource
    private ApiTaskPoolMapper apiTaskPoolMapper;
    @Resource
    private InterRecMapper interRecMapper;
    @Resource
    private OutRecMapper outRecMapper;
    @Resource
    private ArtRecMapper artRecMapper;
    @Autowired
    private IOutApi iOutApi;

    @Resource
    private SalePartnerMapper salePartnerMapper;
    @Resource
    private SelectPushMapper selectPushMapper;


    @Resource
    private RedisCache redisCache;

    @Autowired
    private CommonApi commonApi;


    @Scheduled(cron = "0 0 8 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    private void selectPushTaskPanzhi() {
        SelectPush selectPush = new SelectPush();
        selectPush.setState("0");
        selectPush.setType("111100000");
        List<SelectPush> selectPushes = selectPushMapper.selectSelectPushList(selectPush);
        for (SelectPush push : selectPushes) {
            push.setType("panzhi");
            selectPushMapper.updateSelectPush(push);
        }
    }

    //    @Scheduled(cron = "0/5 * * * * ?")
//    @Scheduled(cron = "0 0 7 * * ?")
    private void loginPanzhi() {
        SalePartner salePartner = new SalePartner();
        salePartner.setSaleKey("13239355056");
        salePartner.setOther("aa120120");
        String token = commonApi.loginPanzhi(salePartner);
        redisCache.setCacheObject("panzhiToken", token, 48, TimeUnit.HOURS);
    }

    @Scheduled(cron = "0 0 0/1 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    private void delFailPanzhi() {

        SalePartner salePartner = salePartnerMapper.selectSalePartnerById(55l);

        OutRec outRec = new OutRec();
        outRec.setSalecode(salePartner.getSaleName());
        outRec.setErrCount(1);
        outRec.setInter("add");
        outRec.setRes("请先通过发布安全校验");
//        outRec.setType("枪战王者");
        List<OutRec> outRecs = outRecMapper.selectOutRecList(outRec);
        for (OutRec rec : outRecs) {
            String accountId = rec.getAccountid().toString();
            interRecMapper.deleteInterAccount(accountId, salePartner.getSaleName());
            outRecMapper.deleteOutRecById(rec.getId());
            artRecMapper.deleteArtRecCode(Long.parseLong(accountId), salePartner.getId());
        }
    }

}
